package com.zw.mes.module.tecmake.controller;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.technology.MakeResult;
import com.zw.mes.entity.technology.Result;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.tecmake.service.MakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName PurchasedController.java
 * @Description TODO
 * @createTime 2021年01月29日 09:06:00
 */
@Controller
@RequestMapping("/tecmake")
@SuppressWarnings("all")
public class MakeController extends BaseController {

    @Autowired
    private MakeService makeService;
    @Autowired
    private DictCommonService dictCommonService;

    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 页面跳转到制作件清单分配
     *
     **/
    @GetMapping
    public String listPage(Model model, MakeResult makeResult){
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("sumNum", makeService.getSumNum(makeResult));
        return "technology/make/listPage";
    }

    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 计划下达时更改未下达计划总数和未下达项目总数
     *
     **/
    @GetMapping("/refreshNum")
    @ResponseBody
    public JsonResult refreshNum1(MakeResult makeResult){
        MakeResult result1 = makeService.getSumNum(makeResult);
        return JsonResult.success("",result1);
    }

    /**
     * @Author mayongfeng
     * @Date 14:24 2021/1/29
     * @Description 查询制作件清单分配列表
     *
     **/
    @PostMapping("/list")
    @ResponseBody
    public JsonResult listPurchased(MakeResult makeResult){
//        startPage();
        List<MakeResult> list = makeService.findWithReportQuery(makeResult);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description 查询没拆分的制作件清单明细
     *
     **/
    @GetMapping("/selectDetail")
    public String selectDetail(Integer id,Integer sheetType,
                               Model model){
        /** 通过Id查询该项目下的标头*/
        model.addAttribute("selectOne", makeService.getSelectById(id).get(0));
        /** 查询明细*/
        model.addAttribute("selectAll", makeService.getSelectDetail(id,sheetType));
        return "technology/make/selectPage";
    }

      /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description 查询拆分后的制作件清单明细
     *
     **/
    @GetMapping("/selectResult")
    public String selectDetail(String prodNo, Model model){
        /** 通过Id查询该项目下的标头*/
        model.addAttribute("selectOne", makeService.getResultDetail(prodNo).get(0));
        /** 查询明细*/
        model.addAttribute("selectAll", makeService.getResultDetail(prodNo));
        return "technology/make/selectPage";
    }




    /**
     * @Author mayongfeng
     * @Date 13:13 2021/2/21
     * @Description 拆分页面
     *
     **/
    @GetMapping("/split/{id}")
    public String splitPage(@PathVariable("id") Integer id,Model model){
        /** 通过Id查询该项目下的标头*/
        model.addAttribute("selectOne", makeService.getSelectById(id).get(0));
        model.addAttribute("selectList", makeService.getSelectById(id));
        return "technology/make/splitPage";
    }

    /**
     * @Author mayongfeng
     * @Date 8:31 2021/2/22
     * @Description 查询拆分页面的明细详情
     *
     **/
    @PostMapping("/planList")
    @ResponseBody
    public JsonResult planList(MakeResult result){
        startPage();
        List<MakeResult> list = makeService.findPlanListQuery(result);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 8:31 2021/2/22
     * @Description 自动拆分功能
     *
     **/
    @PostMapping("/autoSplit")
    @ResponseBody
    public JsonResult autoSplit(MakeResult makeResult){
        startPage();
        List<MakeResult> list = makeService.findautoSplit(makeResult);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 8:31 2021/2/22
     * @Description 自动拆分功能提交到输出表转给工艺
     *
     **/
    @PostMapping("/confirm")
    @ResponseBody
    public JsonResult autoSplitCommit(String allTableData){
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        List<MakeResult> prodPlaDetList = gson.fromJson(allTableData, new TypeToken<List<MakeResult>>() {}.getType());
        int b = makeService.isSumNum(prodPlaDetList);
        /** 验证相等*/
        if(b==-2){
            return JsonResult.success("手动分配的数量和,不等于提报计划的总数量,请检查!!!", -2);
        }

        return jsonResult(b);
    }

    /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description 主表下达数据添加到输出表
     *
     **/
    @PostMapping("/sheetRelease")
    @ResponseBody
    public JsonResult sheetRelease(Integer resultAndSheetId,String prodNo){
        int i = makeService.addResultAndModifyLssue(resultAndSheetId);
        return jsonResult(i);
    }

    /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description 主表变更数据下达并且修改到输出表
     *
     **/
    @PostMapping("/chaSheetRelease")
    @ResponseBody
    public JsonResult chaSheetRelease(String row){
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        MakeResult makeResult = gson.fromJson(row, MakeResult.class);
        int i = makeService.chaSheetRelease(makeResult);
        /** 判断I的状态*/
        if(i==-2){
            return JsonResult.success("请下达原始单或者上一个变更单，再下达此变更单！",-2);
        }
        return jsonResult(i);
    }



    /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description 在输出表更改下达的数据的状态
     *
     **/
    @PostMapping("/resultRelease")
    @ResponseBody
    public JsonResult resultRelease(Integer resultAndSheetId,String prodNo){
        int i = makeService.modifyLssue(resultAndSheetId,prodNo);
       return jsonResult(i);
    }







}
